package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.voicechat.firstmet.App;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.GifBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: 他人礼物列表
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/9/4 14:11
 * @Version: 1.0
 */
public class GifListAdapter extends BaseAdapter<GifBean.PageBean.ListBean>{
    private Context context;
    private  List<GifBean.PageBean.ListBean> tlist;
    Thread thread;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                Bundle data = msg.getData();
                byte[] bitmap = data.getByteArray("bitmap");
                //String tag = data.getString("tag");
                ImageView view = (ImageView) msg.obj;
                GlideLoadUtils.getInstance().glideLoad(App.getContext(),bitmap,view);
            }
        }
    };
    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public GifListAdapter(Context context, List<GifBean.PageBean.ListBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
        this.context = context;
        this.tlist = tlist;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        ViewHolder1 viewHolder1 = new ViewHolder1(view);

        return viewHolder1;
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
       if(viewHolder instanceof ViewHolder1){
           ViewHolder1 viewHolder1 = (ViewHolder1) viewHolder;
           GifBean.PageBean.ListBean listBean = tlist.get(position);
           viewHolder1.tvCharm.setText("+"+listBean.getCharm()+"魅力值");
           viewHolder1.tvGifName.setText(listBean.getName());
           viewHolder1.tvGifNum.setText("数量："+listBean.getNumber());
          // setImage(listBean.getGiftUrl(),viewHolder1.imageGif);
           if(!TextUtils.isEmpty(listBean.getGiftUrl())) {
               if (!listBean.getGiftUrl().equals(viewHolder1.imageGif.getTag(R.id.image_gif))) {
                   // 加载图片
                   viewHolder1.imageGif.setTag(R.id.image_gif, listBean.getGiftUrl());
                   GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + listBean.getGiftUrl(),
                           viewHolder1.imageGif);
                   //setImage(listBean.getAvatarUrl(), moCardViewHolder.imageIcon);
               }
           }

        }
    }
    /**
    * @method
    * @description 请求礼物图片
    * @date: 2018/11/17 10:50
    * @author: ljp
    * @return
    */
    private void setImage(String url, View view) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OssClient.getInstance().init(context).fileDownload(url, new OssClient.OnDownloadFile() {
                    @Override
                    public void onDownFileSuccess(InputStream inputStream) {
                        byte[] bitmap = null;
                        try {
                            bitmap = OssConfig.readInputStreamToByte(inputStream);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //if(view.getTag() != null && view.getTag().equals(url)) {
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = view;
                        Bundle bundle = new Bundle();
                        //bundle.putString("tag",url);
                        bundle.putByteArray("bitmap", bitmap);
                        message.setData(bundle);
                        handler.sendMessage(message);
                        //  }
                    }
                    @Override
                    public void onDownFileProgress(String progress) {
                    }
                });
            }
        }).start();

    }
    /**
     * 布局1
     */
    class ViewHolder1 extends BaseViewHolder{
        @BindView(R.id.image_gif)
        ImageView imageGif;
        @BindView(R.id.tv_gif_name)
        TextView tvGifName;
        @BindView(R.id.tv_gif_num)
        TextView tvGifNum;
        @BindView(R.id.tv_charm)
        TextView tvCharm;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
